package com.key.org.action;

import com.key.base.action.BaseAction;
import com.key.org.model.Department;
import com.key.org.service.IDepartmentService;
import com.key.util.XspMessage;

public class DepartmentAction extends BaseAction {
	IDepartmentService departmentService;
	private Department department;

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public IDepartmentService getDepartmentService() {
		if (departmentService == null)
			departmentService = (IDepartmentService) getBean("departmentServiceImp");
		return departmentService;
	}

	public void setDepartmentService(IDepartmentService DepartmentService) {
		this.departmentService = departmentService;
	}

	@Override
	public String save() {
		// TODO Auto-generated method stub
		XspMessage xspMessage = new XspMessage();
		try {
			// /
			if (department.getLeaderPost().getId().equals("")) {
				department.setLeaderPost(null);
			}
			if (department.getUpperDepartment().getId().equals("")) {
				department.setUpperDepartment(null);
			}
			getDepartmentService().add(department);
		} catch (Exception e) {
			String s = xspMessage.setMessage("保存失败", e.toString(),
					XspMessage.ERROR);
			return s;
		}
		return "Success";
	}

	@Override
	public String update() {
		// TODO Auto-generated method stub
		XspMessage xspMessage = new XspMessage();
		try {
			// /
			if (department.getLeaderPost().getId().equals("")) {
				department.setLeaderPost(null);
			}
			if (department.getUpperDepartment().getId().equals("")) {
				department.setUpperDepartment(null);
			}
			getDepartmentService().update(department);
		} catch (Exception e) {
			String s = xspMessage.setMessage("更新失败", e.toString(),
					XspMessage.ERROR);
			return s;
		}
		return "Success";
	}
}
